package gui;


import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import source.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author padsilva
 */
public class MenuLogin extends javax.swing.JFrame {

    DatabaseHandler connect;

    /**
     * Creates new form MenuLogin
     */
    public MenuLogin(DatabaseHandler conn) {
        connect = conn;
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screenSize.width - this.getWidth()) / 2, (screenSize.height - this.getHeight()) / 2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label1 = new javax.swing.JLabel();
        nomeUtilizador = new javax.swing.JTextField();
        label2 = new javax.swing.JLabel();
        botaoParaUtilizadorMenuPrincipal = new javax.swing.JButton();
        botaoParaMenuEntrada = new javax.swing.JButton();
        password = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        label1.setText("Insira o nome de utilizador:");

        nomeUtilizador.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nomeUtilizadorMouseClicked(evt);
            }
        });
        nomeUtilizador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeUtilizadorActionPerformed(evt);
            }
        });

        label2.setText("Insira a password:");

        botaoParaUtilizadorMenuPrincipal.setText("LOGIN");
        botaoParaUtilizadorMenuPrincipal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoParaUtilizadorMenuPrincipalActionPerformed(evt);
            }
        });

        botaoParaMenuEntrada.setText("Voltar");
        botaoParaMenuEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoParaMenuEntradaActionPerformed(evt);
            }
        });

        password.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                passwordMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(botaoParaUtilizadorMenuPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                        .addComponent(botaoParaMenuEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label1)
                            .addComponent(label2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nomeUtilizador, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                            .addComponent(password))))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label1)
                    .addComponent(nomeUtilizador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label2)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoParaUtilizadorMenuPrincipal)
                    .addComponent(botaoParaMenuEntrada))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoParaUtilizadorMenuPrincipalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoParaUtilizadorMenuPrincipalActionPerformed
        // TODO add your handling code here:
        
        if(nomeUtilizador.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'nome de utilizador' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(password.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'password' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        String result = null;
        try {
            result = connect.checkUser(nomeUtilizador.getText(), PasswordEncrypt.MD5(password.getText()));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(MenuLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MenuLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        String user=nomeUtilizador.getText();
        int id=connect.getId(user);
        if (result.equals("TRUE")) {
            this.setVisible(false);
            UtilizadorMenuPrincipal menu = new UtilizadorMenuPrincipal(connect,user,id);
            menu.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this, "Nome de utilizador / Password errado!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }

//        String username= nomeUtilizador.getText();
//        String pass= password.getText();
//        

    }//GEN-LAST:event_botaoParaUtilizadorMenuPrincipalActionPerformed

    private void botaoParaMenuEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoParaMenuEntradaActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        MenuEntrada menu = new MenuEntrada(connect);
    }//GEN-LAST:event_botaoParaMenuEntradaActionPerformed

    private void nomeUtilizadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeUtilizadorActionPerformed
    }//GEN-LAST:event_nomeUtilizadorActionPerformed

    private void nomeUtilizadorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nomeUtilizadorMouseClicked
        // TODO add your handling code here:
        nomeUtilizador.setText("");
    }//GEN-LAST:event_nomeUtilizadorMouseClicked

    private void passwordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_passwordMouseClicked
        // TODO add your handling code here:
        password.setText("");
    }//GEN-LAST:event_passwordMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoParaMenuEntrada;
    private javax.swing.JButton botaoParaUtilizadorMenuPrincipal;
    private javax.swing.JLabel label1;
    private javax.swing.JLabel label2;
    private javax.swing.JTextField nomeUtilizador;
    private javax.swing.JPasswordField password;
    // End of variables declaration//GEN-END:variables
}
